//###############
// FILE : FileSearch.java
// WRITER : Lior Zimmerman liorz06 066462300
// WRITER : Ohad Dan ohad_dan 036767960
// EXERCISE : oop ex3 2008
// DESCRIPTION:
// Search using gui or command-line.
//###############
package oop.ex3;
import java.io.File;
import oop.ex3.fileSearchView.GUIInitializer;
import oop.ex3.fileSearchModel.SearchEngine.*;
import oop.ex3.fileSearchModel.util.*;
import oop.ex3.fileSearchModel.util.exceptions.*;
import oop.ex3.fileSearchModel.filters.exceptions.*;
import oop.ex3.fileSearchModel.parser.exceptions.*;
import java.io.IOException;
import java.util.ArrayList;

/**
 * Search using gui or command-line.
 */
public class FileSearch {
	//********************************************************************
	//  Finals :                                                         *
	//********************************************************************
	private static final int GUI_MODE = 0;
	private static final int KEYBOARD_MODE = 1;
	private static final String INVALID_INPUT_MESSAGE = "Error:Invalid " +
			"command line parameters";
	private static final String IO_ERROR_MSG = "Error:I/O failure";
	//********************************************************************
	//  Methods :                                                        *
	//********************************************************************	
	/**
	 * Search using gui or command-line.
	 * @param args Empty in case of gui use or,
	 *  	args[0] - source directory.
	 *  	args[1] - filter file.
	 */
	public static void main(String[] args) {
		int operationMode = operationMode(args);
		if (operationMode == GUI_MODE){
			guiSearch();
		}
		else{			
			keyboardSearch(args);
		}	
	}
	/**
	 * If the input data is valid, return operation mode.
	 * @param args Input.
	 * @return True if the input data is valid, false otherwise.
	 */
	private static int operationMode(String[] args){
		if (args.length == 0){
			return GUI_MODE;
		}
		else if (args.length == 2){
			 File source  = new File(args[0]);
			 File filter = new File(args[1]);
			 if ( filter.exists() && filter.isFile() && source.exists() ){
				 return KEYBOARD_MODE;
			 }
			 else{
				 exit();
			 }
		}
		else{
			exit();			
		}
		return 0;
	}
	/**
	 * Exits on error.
	 */
	private static void exit(){
		 System.err.println(INVALID_INPUT_MESSAGE);
		 System.exit(1);
	}
	/**
	 * Initialize gui search.
	 */
	private static void guiSearch(){
		GUIInitializer mainFrame = new GUIInitializer();
		mainFrame.setListeners();
		mainFrame.setVisible(true);
	}
	/**
	 * Initialize keyboard search.
	 * @param args Empty in case of gui use or,
	 *  	args[0] - source directory.
	 *  	args[1] - filter file.
	 */
	private static void keyboardSearch(String[] args){
		try{
			 SearchEngine searcher = new SearchEngine(args[0]);
			 FilterReader reader = new FilterReader(args[1]);
			 FileIterator it = reader.iterator();
			 while ( it.hasNext() ) {
		            String filterExp = it.next();
		            System.out.println(filterExp);
		            ArrayList<String> files = searcher.search(filterExp);
		            for (String fltr:files){
		            	System.out.println(fltr);
		            }
			 }		
	         reader.close();
			}
			catch( ReaderIOException e){
				System.err.println(e.getMessage());
			}
			catch( FilterException e){
			    System.err.println(e.getMessage());
				}
			catch( ParsingException e){
				System.err.println(e.getMessage());
			}
			catch( IOException e){
				System.out.println(IO_ERROR_MSG);
			}
	}
}
